package com.cml.domain.factory;

import com.cml.domain.entity.DefaultMessage;
import com.cml.domain.entity.Message;

import java.io.DataInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * 姓名：陈茂林
 * 时间：2023/10/11 9:08
 * 描述：
 */
public class DefaultMessageBuilderFactory extends  MessageBuilderFactory{



    @Override
    public Message doBuildMessage(DataInputStream dataInputStream) throws IOException {
        DefaultMessage defaultMessage = new DefaultMessage();
        defaultMessage.setDefaultMessageType(dataInputStream.readInt());
        int messageLength = dataInputStream.readInt();
        byte[] b=new byte[messageLength];
        dataInputStream.read(b);
        defaultMessage.setDefaultMessage(b);
        return defaultMessage;
    }

    @Override
    public void doMessageBuilder(ByteBuffer byteBuffer, Message message) throws IOException {
        DefaultMessage defaultMessage = (DefaultMessage) message;
        byteBuffer.putInt(defaultMessage.getDefaultMessageType());
        byteBuffer.putInt(defaultMessage.getDefaultMessage().length);
        byteBuffer.put(defaultMessage.getDefaultMessage());
    }


}
